home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
167_01
/
spline.doc
< prev
Wrap
Text File
|
1985-11-13
|
21KB
|
534 lines
/*
HEADER: CUG
TITLE: Cubic Spline Functions Theory;
VERSION: 3.00;
DATE: 09/26/86;
DESCRIPTION: "Mathematical background and development of
equations used in SPLINE, a full emulation of
Unix's 'spline' utility.";
KEYWORDS: spline, graphics, plot, filter, UNIX;
SYSTEM:
FILENAME: SPLINE.DOC;
WARNINGS:
CRC: xxxx
SEE-ALSO: SPLINE.C;
AUTHORS: Ian Ashdown - byHeart Software;
COMPILERS:
REFERENCES: AUTHORS: R.W. Hamming;
TITLE: Numerical Methods for Scientists and
Engineers, 2nd Edition
pp. 349 - 355, McGraw-Hill (1973);
AUTHORS: Forsythe, Malcolm & Moler;
TITLE: Computer Methods for Mathematical
Computations
pp. 70 - 83, Prentice-Hall;
ENDREF
*/
byHeart Software
620 Ballantree Road
West Vancouver, B.C.
Canada V7S 1W3
September 13th, 1986
Curve Fitting With Cubic Splines
--------------------------------
Ian E. Ashdown
byHeart Software
========================================================================
Abstract: CURVE FITTING WITH CUBIC SPLINES
------------------------------------------
A rigorous development of the mathematical theory of cubic
splines and their use in curve fitting in two and three dimensions.
Emphasis is on the implementation of efficent computer algorithms to
calculate coefficients of both nonperiodic and periodic splines using
Gaussian Elimination and sparse matrix representations. Accompanying
program listing presents C source code for a full emulation of the UNIX
(registered trademark Bell Laboratories) SPLINE curve interpolation
utlity.
========================================================================
Before computer-aided drafting workstations completely replace the
draftsman's pencil and paper, let's examine one of his tools: the
spline. Presented with data in the form of points on an x-y plane, the
draftsman will use a spline - a flexible strip of metal or plastic - to
draw a smooth curve between them.
The technique is very simple. After plotting the data on a sheet of
paper, an appropriately sized spline is held in place at these points
(referred to as "knots") with weights or pins. The draftsman then traces
the curve formed by the spline. For any given set of knots, the curve
generated is independent of the spline chosen, and is thus exactly
reproducible.
From mechanical engineering, elementary beam theory shows that if
the spline is not too severely stressed, it will conform to a curve
described by a set of cubic polynomial equations, one between each pair
of adjacent knots. Adjacent polynomials meet at their common endpoints
(the knots), and their slopes and rates of curvature at these points are
equal. Stated in mathematical terms, these polynomials join continuously
at the knots with continuous first and second derivatives.
Knowing this, we can develop a mathematical model of the draftsman's
splines, and from this model construct a computer program for
interpolating a smooth curve between a set of knots. With a bit of
care in choosing algorithms, such a program can quickly and accurately
generate a curve for a thousand or more data points on the smallest of
personal computers. It can even be adapted to interpolate a smooth
surface between points plotted in three dimensions.
Developing the model involves basic calculus and matrix theory. If
you are unfamiliar with such mathematics, rest assured that the
resultant algorithms are very easy to program, and using a cubic spline
program requires no understanding of the underlying mathematical theory.
Give the program a set of knots and it will dutifully interpolate a
smooth curve in all (well, almost) cases.
Why then discuss the mathematics of cubic splines at all? There are
two answers. One is that seeing how the algorithms are developed gives
you the confidence to use them. The other is that there may be cases
where the algorithms will not perform exactly as desired. Knowing their
theory may enable you to create a modified algorithm to fit the problem
at hand.
A Simple Explanation
--------------------
Whether you understand calculus and matrices or not, it helps to
have in mind a mental image of what you are trying to accomplish. Even
if you aren't comfortable with the mathematics, a conceptual model will
serve to illustrate the principles involved.
A cubic polynomial equation is nothing more than a simple algebraic
equation of the form:
3 2
y = ax + bx + cx + d
where a,b,c and d are constants. If you plot the knots on a grid and
actually bend a draftsman's spline to fit between them, each section of
the spline between adjacent knots can be matched by a curve generated
by the above equation ... if the appropriate constants are chosen. The
trick is to find those constants for each section of the spline.
Common sense says three things about these curves. One is that they
should join at the knots. Next, the slopes of the curves should be the
same where they join. Third, the curvature of the curves where the join
should be the same. The first is obvious - the curves must form a
continuous line. The second and third are more intuitive, but a few
moments thought should convince you. A semirigid object such as a
spline does not permit abrupt changes in angle or curvature as it is
bent around a rigid pin.
Stating these ideas mathematically will enable you to calculate the
constants for all the cubic polynomial equations needed to model the
spline.
A Rigorous Explanation
----------------------
Beginning in this section, the mathematical theory of cubic splines
will be rigorously developed. If this approach does not appeal to you,
simply skim the text for the information on how to implement the spline
algorithms, then examine the accompanying program listing. The program
header provides all the information you will need to use the program.
Starting with a set of data points (the knots) stated as horizontal
coordinates x[i] (i = 1,...,n) and corresponding vertical coordinates
y[i], the curve-to-be is defined as the composite function:
y(x) = f[i](x) for x[i] <= x < x[i+1], i = 1,...,n-2
and x[n-1] <= x <= x[n]
where each function f[i](x) is a cubic polynomial as described above.
In other words, y(x) is really a set of functions, each of which is
defined over an interval between two adjacent knots at (x[i],y[i]) and
(x[i+1],y[i+1]).
Furthermore, let's define y'[i] and y"[i] as the first and second
derivatives of y(x) at x = x[i]. Knowing that the set of functions
f[i](x) must join at their endpoints (the knots of the spline) and also
that their first and second derivatives are continuous at these points,
we have the following continuity conditions:
f[i](x[i]) = y[i] i = 1,...,n-1
f[i-1](x[i]) = y[i] i = 2,...,n
f'[i-1](x[i]) = f'[i](x[i]) i = 2,...,n-1
f"[i-1](x[i]) = f"[i](x[i]) i = 2,...,n-1
Since each function f[i](x) is a cubic polynomial, it follows that
its second derivative is a linear function (a straight line) between its
endpoints. If we define:
h[i] = x[i+1] - x[i]
then linear interpolation gives us:
y"[i] * (x[i+1] - x) + y"[i+1] * (x - x[i])
f"[i](x) = -------------------------------------------
h[i]
Integrating this equation twice and selecting the constants of
integration such that the continuity conditions are satisfied, we can
derive the following interpolation equation:
y[i] * (x[i+1] - x) + y[i+1] * (x - x[i])
f[i](x) = ----------------------------------------- -
h[i]
2 +- +-
h[i] | | (x[i+1] - x)
---- * | y"[i] * |------------- -
6 | | h[i]
+- +-
+- -+ 3 -+ +-
| x[i+1] - x | | | (x - x[i])
| ---------- | | + y"[i+1] * | ---------- -
| h[i] | | | h[i]
+- -+ -+ +-
+- -+ 3 -+ -+
| x - x[i] | | |
| -------- | | |
| h[i] | | |
+- -+ -+ -+
for i = 1,...,n-1
Interpo